<?php

namespace App\Http\Controllers\WechatFront;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;

class WallController extends Controller
{
    public function index($id)
    {
        //查询
        $wall = DB::table('walls')
                    ->where('id', '=', $id)
                    ->select('title', 'qr_code','background')
                    ->first();
        $title = $wall->title;
        $qrUrl = $wall->qr_code;
        $background = $wall->background;

        return view(
            'wechatfront.wall.index',
                    ['title'=>$title
                    ,'qrurl'=>$qrUrl
                    ,'background'=>$background
                    ,'id'=>$id]
        );
    }

    public function ajaxWallInfo($wallid, $lastid)
    {
        //查询微信墙信息
        $wallInfo = DB::table('wall_chats')
                    ->join('wechat_users', 'wall_chats.openid', '=', 'wechat_users.openid')
                    ->select('wall_chats.id', 'wechat_users.nickname', 'wechat_users.headimgurl', 'wall_chats.value')
                    ->where([['wall_chats.wallid','=',$wallid],['wall_chats.id','>',$lastid]])
                    ->orderBy('wall_chats.id')
                    ->get();
        return response()->json($wallInfo);
    }

    public function draw($wallid)
    {
        //查询用户
        $users = DB::table('wechat_users')
                ->select('openid', 'nickname', 'headimgurl')
                ->where('wallid', '=', $wallid)
                ->get();
        return view(
            'wechatfront.wall.draw',
                    ["users"=>$users,"id"=>$wallid]
        );
    }

    public function showQrCode($id)
    {
        //查询qrcode地址
        $qrcode = DB::table('walls')
                    ->where('id', '=', $id)
                    ->value('qr_code');
        return view(
            'wechatfront.wall.showqrcode',
            ['qrcode'=>$qrcode]
        );
    }
}
